package com.pu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pu.constant.MessageConstant;
import com.pu.constant.StatusConstant;
import com.pu.context.BaseContext;
import com.pu.dto.CategoryDTO;
import com.pu.dto.CategoryPageQueryDTO;
import com.pu.entity.Category;
import com.pu.entity.Dish;

import com.pu.entity.Setmeal;
import com.pu.exception.DeletionNotAllowedException;
import com.pu.exception.SetmealEnableFailedException;
import com.pu.mapper.CategoryMapper;
import com.pu.mapper.DishMapper;
import com.pu.mapper.SetmealMapper;
import com.pu.result.PageResult;
import com.pu.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author Yusc
 * @Date 2023/10/26 15:25
 * @Version
 * @Description 菜品分类业务层
 **/
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * @param categoryDTO
     * @return
     * @auther: Yusc
     * @date 2023/10/26 15:35
     * @Description 新增分类
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);

        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);

        category.setCreateTime(LocalDateTime.now());    //创建时间
        category.setUpdateTime(LocalDateTime.now());    //修改时间
        category.setCreateUser(BaseContext.getCurrentId()); //创建人
        category.setUpdateUser(BaseContext.getCurrentId()); //修改人

        if (save(category)) {
            log.info("新增成功!操作人:{}", BaseContext.getCurrentId());
        }
        ;
    }

    /**
     * @param categoryPageQueryDTO
     * @auther: Yusc
     * @date 2023/10/26 15:49
     * @Description 分类分页查询
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 开启分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        // 自动加入limit关键字分页
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * @param id
     * @auther: Yusc
     * @date 2023/10/26 16:21
     * @Description 删除分类
     */
    @Override
    public void deleteById(Long id) {
        // 1.构建查询条件
        LambdaQueryWrapper<Dish> dWrapper = new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, id);
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Long count = dishMapper.selectCount(dWrapper);
        log.info("要删除的菜品分类:{}，关联了{}个菜品", id, count);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, id);
        count = setmealMapper.selectCount(wrapper);
        log.info("要删除的菜品分类:{}，关联了{}个套餐", id, count);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.deleteById(id);
    }

    /**
     * @param categoryDTO
     * @auther: Yusc
     * @date 2023/10/26 16:50
     * @Description 修改菜品分类
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category); //拷贝属性

        //设置修改时间、修改人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        //执行lambda语句更新指定id
        lambdaUpdate().eq(Category::getId, category.getId()).update(category);
    }

    /**
     * @auther: Yusc
     * @date 2023/10/26 16:56
     * @Description 启用、禁用分类
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = new Category();
        category.setId(id);     // 获取id
        category.setStatus(status); //状态
        category.setUpdateTime(LocalDateTime.now());    //更新时间
        category.setUpdateUser(BaseContext.getCurrentId()); //更新人id


        lambdaUpdate().eq(Category::getId, category.getId())
                .update(category);
    }

    /**
     * @param type
     * @auther: Yusc
     * @date 2023/10/26 17:06
     * @Description 根据类型查询分类
     */
    @Override
    public List<Category> Categorylist(Integer type) {
        return lambdaQuery().eq(Category::getType, type).list();
    }
}
